﻿//设置编译变量
//#define debug

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Etong.Web;
public partial class Admin_Edit : System.Web.UI.Page
{
    public override string StyleSheetTheme
    {
        get
        {
            //return base.StyleSheetTheme;
            return ConfigGlobal.GetGlobal().Theme;
        }
        set
        {
            base.StyleSheetTheme = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ConfigGlobal.CheckLog();
        ConfigTable cTab =ConfigTable.GetThisPageConfigTable();
        bool isEdit = false;
        string pkValue = string.Empty;
        
        if (Request.QueryString["type"].ToLower() == "edit")
        {
            isEdit = true;
            pkValue = Request.QueryString["id"];            
        }
        
        cTab.SetUpEditPanel(this.EditPanel, pkValue, isEdit);
        if (!Page.IsPostBack)
        {
            if (Request.UrlReferrer != null)
            {
                ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
            }
        }

        this.AddButton.Visible = !isEdit;
        this.EditButton.Visible = isEdit;
    }
    
    protected void AddButton_Click(object sender, EventArgs e)
    {
        ConfigTable cTab =ConfigTable.GetThisPageConfigTable();
        #if debug
        Response.Write(cTab.GetInsertSql());
        #else
        BeforeFactory.BeforeDo(cTab, BeforeType.BeforeInsert, string.Empty);
        DBFactory.GetConn().exe0(cTab.GetInsertSql());
        AfterFactory.AfterDo(cTab, AfterType.AfterInsert,string.Empty);
        #endif

        string url = cTab.ListUrl;
        if (ViewState["UrlReferrer"] != null && !string.IsNullOrEmpty(ViewState["UrlReferrer"].ToString()))
        {
            url = ViewState["UrlReferrer"].ToString();
        }

            #if debug
            Response.Write(url);
            #else
            Response.Redirect(url);
            #endif
    }
    protected void EditButton_Click(object sender, EventArgs e)
    {
        ConfigTable cTab = ConfigTable.GetThisPageConfigTable();
        string id = Request.QueryString["id"];
        #if debug
        Response.Write("执行："+cTab.GetUpdateSql(Request.QueryString["id"]));
        #else
        BeforeFactory.BeforeDo(cTab, BeforeType.BeforeUpdate, id);
        DBFactory.GetConn().exe0(cTab.GetUpdateSql(id));
        AfterFactory.AfterDo(cTab, AfterType.AfterUpdate,id);
        #endif

        string url = cTab.ListUrl;
        if (ViewState["UrlReferrer"] != null && !string.IsNullOrEmpty(ViewState["UrlReferrer"].ToString()))
        {
            url = ViewState["UrlReferrer"].ToString();
        }
        if (cTab.SingleType)
        {
            url = Request.Url.ToString();
        }

        if (cTab.SingleType)
        {
            #if debug
            Helper.Alert("修改成功","sucAlert");
            #else
            Helper.Alert("修改成功", "sucAlert", url);
            #endif
        }
        else
        {
            #if debug
            Response.Write("<br>转向:"+url);
            #else
            Response.Redirect(url);
            #endif
        }
    }
}
